﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;

namespace FeatureCenter.Module.InfoPanels {
    public abstract class InfoPanelsViewControllerBase<TemplateType> : CustomizeTemplateViewControllerBase<TemplateType> where TemplateType : IFrameTemplate {
        private PopupWindowShowAction action;
        private Frame relatedViewFrame;
        private string GetInfoText() {
            string viewRelatedInfo = View.Caption;
            string viewModelRelatedInfo = ((IModelViewDemoDescription)View.Model).DemoDescription;
            string currentObjectRelatedInfo;
            if(View is ListView || View.CurrentObject == null) {
                currentObjectRelatedInfo = Hints.FrameTemplates_InfoPanels_ListViewHintTemplate;
            } else {
                currentObjectRelatedInfo = string.Format(Hints.FrameTemplates_InfoPanels_DetailViewHintTemplate, ((InfoPanelsObject)View.CurrentObject).DataProperty);
            }
            return string.Format(Hints.FrameTemplates_InfoPanels_RightPanelTextTemplate, viewRelatedInfo, viewModelRelatedInfo, currentObjectRelatedInfo);
        }
        private void action_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) {
            IObjectSpace objectSpace = Application.CreateObjectSpace();
            DetailView detailView = Application.CreateDetailView(objectSpace, objectSpace.GetObject(View.CurrentObject));
            e.View = detailView;
            e.Context = TemplateContext.PopupWindow;
        }
        private void view_ControlsCreated(object sender, EventArgs e) {
            ((View)sender).ControlsCreated -= new EventHandler(view_ControlsCreated);
            if(relatedViewFrame != null && relatedViewFrame.Template is ISupportStoreSettings) {
                ISupportStoreSettings storeSettings = (ISupportStoreSettings)relatedViewFrame.Template;
                storeSettings.SetSettings(Application.GetTemplateCustomizationModel(relatedViewFrame.Template));
                storeSettings.ReloadSettings();
            }
        }
        private void View_ModelSaved(object sender, EventArgs e) {
            if(relatedViewFrame != null && relatedViewFrame.Template is ISupportStoreSettings) {
                ((ISupportStoreSettings)relatedViewFrame.Template).SaveSettings();
            }
        }
        protected abstract void SetInfoTextToControls(string text);
        protected object CreateRelatedViewControl() {
            if(relatedViewFrame == null) {
                relatedViewFrame = Application.CreateFrame(TemplateContext.NestedFrame);
                relatedViewFrame.CreateTemplate();
                IObjectSpace objectSpace = Application.CreateObjectSpace();
                DetailView view = Application.CreateDetailView(objectSpace, "InfoPanelsObject_Context_DetailView", false, objectSpace.GetObject(View.CurrentObject));
                view.ControlsCreated += new EventHandler(view_ControlsCreated);
                view.AllowEdit["Demo"] = false;
                relatedViewFrame.SetView(view);
            } else {
                if(NeedRecreateRelatedViewFrameTemplate) {
                    relatedViewFrame.View.BreakLinksToControls();
                    relatedViewFrame.SetTemplate(null);
                    relatedViewFrame.CreateTemplate();
                }
            }
            return relatedViewFrame.Template;
        }
        protected override void UpdateControls(View view) {
            SetInfoTextToControls(GetInfoText());
        }
        protected override void UpdateControls(object currentObject) {
            SetInfoTextToControls(GetInfoText());
            if(relatedViewFrame != null) {
                relatedViewFrame.View.CurrentObject = currentObject == null ? null : relatedViewFrame.View.ObjectSpace.GetObject(currentObject);
            }
        }
        protected override void OnActivated() {
            base.OnActivated();
            View.ModelSaved += new EventHandler(View_ModelSaved);
        }
        protected override void OnDeactivated() {
            base.OnDeactivated();
            View.ModelSaved -= new EventHandler(View_ModelSaved);
        }
        public InfoPanelsViewControllerBase() {
            TargetObjectType = typeof(InfoPanelsObject);
            action = new PopupWindowShowAction(this, "ContextAction", "ContextActions");
            action.Caption = Hints.FrameTemplates_InfoPanels_OpenCurrentObjectAction;
            action.SelectionDependencyType = SelectionDependencyType.RequireSingleObject;
            action.CustomizePopupWindowParams += new CustomizePopupWindowParamsEventHandler(action_CustomizePopupWindowParams);
            RegisterActions(action);
        }
        protected virtual bool NeedRecreateRelatedViewFrameTemplate {
            get { return false; }
        }
    }
}
